//
//  UIBarButtonItem+RTInit.m
//  WeiboSina
//
//  Created by Brisinga on 16/3/4.
//  Copyright © 2016年 Root. All rights reserved.
//

#import "UIBarButtonItem+RTInit.h"

@implementation UIBarButtonItem (RTInit)

+ (instancetype)buttonItemWithImageName:(nullable NSString *)imageName title:(nullable NSString *)title addTarget:(nullable id)target action:(nullable SEL)action {
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] init];
    
    UIButton *barButton = [[UIButton alloc] init];
    
    // 添加按钮点击事件
    [barButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    // 如果图片存在则添加图片
    if (imageName) {
        
        [barButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        
        [barButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted",imageName]] forState:UIControlStateHighlighted];
    }
    
    // 如果title存在则添加title
    if (title) {
        
        [barButton setTitle:title forState:UIControlStateNormal];
        
        [barButton setTitleColor:[UIColor colorWithRed:52/255.0 green:52/255.0 blue:52/255.0 alpha:1] forState:UIControlStateNormal];
        
        [barButton setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
        
        barButton.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    
    [barButton sizeToFit];
    
    item.customView = barButton;
    
    return item;
}

@end
